package com.icinfo.cloud.provider.peripheralinterface.lawenforce.service.impl;

import cn.hutool.core.io.resource.InputStreamResource;
import cn.hutool.http.ContentType;
import cn.hutool.http.HttpStatus;
import cn.hutool.http.HttpUtil;
import cn.hutool.http.Method;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.icinfo.cloud.provider.common.constant.Constant;
import com.icinfo.cloud.provider.common.exception.BusinessException;
import com.icinfo.cloud.provider.common.utils.FileUtil;
import com.icinfo.cloud.provider.peripheralinterface.lawenforce.dto.CaseBaseInfoQueryDto;
import com.icinfo.cloud.provider.peripheralinterface.lawenforce.dto.CaseBaseInfoSaveDto;
import com.icinfo.cloud.provider.peripheralinterface.lawenforce.dto.CaseBaseInfoStatusUpdateDto;
import com.icinfo.cloud.provider.peripheralinterface.lawenforce.service.ILawenforceBusinessService;
import com.icinfo.cloud.provider.punish.api.dto.CaseBaseInfoResponse;
import com.icinfo.cloud.provider.punish.api.dto.FileInfoDto;
import com.icinfo.cloud.provider.punish.ucase.punishinform.service.ICaseCriminalTransferService;
import com.icinfo.framework.common.web.Result;
import com.icinfo.framework.tools.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.util.HashMap;

/**
 * 描述：两法服务接口
 *
 * @Author zhengqiang
 * @Date 2023/1/6 11:16
 **/
@Slf4j
@Service
public class LawenforceBusinessServiceImpl implements ILawenforceBusinessService {

    @Value("${lawenforce.url}")
    private String lawenforceUrl;

    @Value("${lawenforce.api.getTransferCaseBaseDetail}")
    private String getTransferCaseBaseDetailApi;
    @Value("${lawenforce.api.saveTransferCaseInfo}")
    private String saveTransferCaseInfoApi;
    @Value("${lawenforce.api.delCaseCommonMessage}")
    private String delCaseCommonMessageApi;
    @Value("${lawenforce.api.readDicListByOwnType}")
    private String readDicListByOwnTypeApi;
    @Value("${lawenforce.api.getAllAreaTree}")
    private String getAllAreaTreeApi;
    @Value("${lawenforce.api.sendTransferDataByCaseBaseInfo}")
    private String sendTransferDataByCaseBaseInfoApi;
    @Value("${lawenforce.api.readCaseBaseInfoByCaseId}")
    private String readCaseBaseInfoByCaseIdApi;
    @Value("${lawenforce.api.updateLawCaseBaseInfoStatus}")
    private String updateLawCaseBaseInfoStatusApi;


    @Value("${document.url}")
    private String documentUrl;
    @Value("${document.api.uploadBase64File}")
    private String uploadBase64File;

    @Resource
    private ICaseCriminalTransferService caseCriminalTransferService;

    /**
     * 描述： 查询案件移送详情
     *
     * @author: zhengqiang
     * @date: 2023/1/5
     * @param: caseBaseId
     * @return {@link Result}
     **/
    @Override
    public Result getTransferCaseBaseDetail(String caseBaseId) {
        log.info("调用两法服务查询案件移送详情接口(getTransferCaseBaseDetail)");
        try {
            HashMap paramMap = new HashMap();
            paramMap.put("caseBaseId", caseBaseId);
            log.info("getTransferCaseBaseDetail -- 入参: {}", paramMap);
            String response = HttpUtil.createGet(lawenforceUrl.concat(getTransferCaseBaseDetailApi)).form(paramMap).execute().body();
            log.info("getTransferCaseBaseDetail -- 出参: {}", response);
            Result result = JSONObject.parseObject(response, Result.class);
            return result;
        } catch (Exception e) {
            log.error("getTransferCaseBaseDetail: {}", e);
            e.printStackTrace();
        }
        return Result.error(Constant.STRING_FAIL_EN);
    }

    /**
     * 描述： 移送案件表 新增数据
     *
     * @author: zhengqiang
     * @date: 2023/1/6
     * @param: caseBaseInfoDto
     * @return {@link Result< String>}
     **/
    @Override
    public Result<String> saveTransferCaseInfo(CaseBaseInfoSaveDto caseBaseInfoDto) {
        //更新详情表移送数据
        caseCriminalTransferService.updateCaseMainDetailInfoCriminal(caseBaseInfoDto);

        log.info("调用两法服务保存案件移送详情接口(saveTransferCaseInfo)");
        try {
            String paramStr = JSONObject.toJSONString(caseBaseInfoDto);
            log.info("saveTransferCaseInfo -- 入参: {}", paramStr);
            String response = HttpUtil.createPost(lawenforceUrl.concat(saveTransferCaseInfoApi)).body(paramStr).execute().body();
            log.info("saveTransferCaseInfo -- 出参: {}", response);
            Result result = JSONObject.parseObject(response, Result.class);
            return result;
        } catch (Exception e) {
            log.error("saveTransferCaseInfo: {}", e);
            e.printStackTrace();
        }
        return Result.error(Constant.STRING_FAIL_EN);
    }

    @Override
    public Result getDicListByOwnType(String ownType) {
        log.info("调用两法服务根据所属类别查询数据列表接口(getDicListByOwnType)");
        try {
            log.info("getDicListByOwnType -- 入参: {}", ownType);
            String response = HttpUtil.createGet(lawenforceUrl.concat(readDicListByOwnTypeApi).concat("/").concat(ownType)).execute().body();
            //log.info("getDicListByOwnType -- 出参: {}", response);
            Result result = JSONObject.parseObject(response, Result.class);
            return result;
        } catch (Exception e) {
            log.error("getDicListByOwnType: {}", e);
            e.printStackTrace();
        }
        return Result.error(Constant.STRING_FAIL_EN);
    }

    /**
     * 描述： 【V1.0.0】通用-省市区-选择树(全国)
     *
     * @author: zhengqiang
     * @date: 2023/1/10
     * @return {@link Result}
     **/
    @Override
    public Result getAllAreaTree() {
        log.info("调用两法服务通用省市区选择树接口(getAllAreaTree)");
        try {
            String response = HttpUtil.createGet(lawenforceUrl.concat(getAllAreaTreeApi)).execute().body();
            //log.info("getAllAreaTree -- 出参: {}", response);
            Result result = JSONObject.parseObject(response, Result.class);
            return result;
        } catch (Exception e) {
            log.error("getAllAreaTree: {}", e);
            e.printStackTrace();
        }
        return Result.error(Constant.STRING_FAIL_EN);
    }

    /**
     * 描述： 删除案件附带数据
     *
     * @author: zhengqiang
     * @date: 2023/1/10
     * @param: messageId
     * @param: messageType
     * @return {@link Result< String>}
     **/
    @Override
    public Result<String> delCaseCommonMessage(String messageId, String messageType) {
        log.info("调用两法服务通用省市区选择树接口(delCaseCommonMessage)");
        try {
            HashMap paramMap = new HashMap();
            paramMap.put("messageId", messageId);
            paramMap.put("messageType", messageType);
            log.info("delCaseCommonMessage -- 入参: {}", paramMap);
            String response = HttpUtil.createGet(lawenforceUrl.concat(delCaseCommonMessageApi)).method(Method.DELETE).form(paramMap).execute().body();
            log.info("delCaseCommonMessage -- 出参: {}", response);
            Result result = JSONObject.parseObject(response, Result.class);
            return result;
        } catch (Exception e) {
            log.error("delCaseCommonMessage: {}", e);
            e.printStackTrace();
        }
        return Result.error(Constant.STRING_FAIL_EN);
    }

    /**
     * 描述： 两法案件移送
     *
     * @author: zhengqiang
     * @date: 2023/1/10
     * @param: caseBaseId
     * @return {@link Result}
     **/
    @Override
    public Result sendTransferDataByCaseBaseInfo(CaseBaseInfoResponse caseBaseInfoResponse) {
        log.info("调用两法服务案件移送接口(sendTransferDataByCaseBaseInfo)");
        try {
            String paramStr = JSONObject.toJSONString(caseBaseInfoResponse);
            log.info("sendTransferDataByCaseBaseInfo -- 入参: {}", paramStr);
            String response = HttpUtil.createPost(lawenforceUrl.concat(sendTransferDataByCaseBaseInfoApi)).body(paramStr).execute().body();
            log.info("sendTransferDataByCaseBaseInfo -- 出参: {}", response);
            Result result = JSONObject.parseObject(response, Result.class);
            return result;
        } catch (Exception e) {
            log.error("sendTransferDataByCaseBaseId: {}", e);
            e.printStackTrace();
        }
        return Result.error(Constant.STRING_FAIL_EN);
    }

    /**
     * 描述： 根据案件id查询两法移送记录
     *
     * @author: zhengqiang
     * @date: 2023/1/10
     * @param: caseId
     * @return {@link CaseBaseInfoQueryDto}
     **/
    @Override
    public CaseBaseInfoQueryDto readCaseBaseInfoByCaseId(String caseId) {
        log.info("调用两法服务保存案件移送详情接口(readCaseBaseInfoByCaseId)");
        try {
            HashMap paramMap = new HashMap();
            paramMap.put("caseBaseId", caseId);
            log.info("readCaseBaseInfoByCaseId -- 入参: {}", paramMap);
            String response = HttpUtil.createGet(lawenforceUrl.concat(readCaseBaseInfoByCaseIdApi)).form(paramMap).execute().body();
            log.info("readCaseBaseInfoByCaseId -- 出参: {}", response);
            Result result = JSONObject.parseObject(response, Result.class);
            if (HttpStatus.HTTP_OK == result.getCode() && !ObjectUtils.isEmpty(result.getData())) {
                CaseBaseInfoQueryDto caseBaseInfoQueryDto = JSON.parseObject(result.getData().toString(), CaseBaseInfoQueryDto.class);
                return caseBaseInfoQueryDto;
            }
            return null;
        } catch (Exception e) {
            log.error("readCaseBaseInfoByCaseId: {}", e);
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 描述： 修改移送案件状态
     *
     * @author: zhengqiang
     * @date: 2023/1/16
     * @param: caseBaseInfoStatusUpdateDto
     * @return {@link Result< String>}
     **/
    @Override
    public Result<String> updateLawCaseBaseInfoStatus(CaseBaseInfoStatusUpdateDto caseBaseInfoStatusUpdateDto) {
        log.info("调用两法服务保存案件移送详情接口(updateLawCaseBaseInfoStatus)");
        try {
            String paramStr = JSONObject.toJSONString(caseBaseInfoStatusUpdateDto);
            log.info("updateLawCaseBaseInfoStatus -- 入参: {}", paramStr);
            String response = HttpUtil.createPost(lawenforceUrl.concat(updateLawCaseBaseInfoStatusApi)).body(paramStr).execute().body();
            log.info("updateLawCaseBaseInfoStatus -- 出参: {}", response);
            Result result = JSONObject.parseObject(response, Result.class);
            return result;
        } catch (Exception e) {
            log.error("updateLawCaseBaseInfoStatus: {}", e);
            e.printStackTrace();
        }
        return Result.error(Constant.STRING_FAIL_EN);
    }
}
